package zj.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.telecom.Call;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zj.com.app.ZjApplication;
import zj.com.interfaze.Callback;
import zj.com.model.InspectContent;
import zj.com.model.InspectItem;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/22.
 */
public class ChoiceExpandableListViewAdapter extends BaseExpandableListAdapter {

    public Context context;
    public LayoutInflater lif;

    public List<InspectItem> list;
    public Map<String,Boolean> checkMap = new HashMap<String,Boolean>();
    public Map<String,InspectItem> contentMap = new HashMap<String,InspectItem>();
    public ChoiceExpandableListViewAdapter(Context context,List<InspectItem> list) {
        this.context = context;
        lif = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getItems().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getItems().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = lif.inflate(
                    R.layout.header, null);
            groupHolder = new GroupHolder();
            groupHolder.name_tv = (TextView) convertView.findViewById(R.id.title_tv);

            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        groupHolder.name_tv.setText(list.get(groupPosition).getRiskName());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ItemHolder itemHolder = null;
        if (convertView == null) {
            convertView = lif.inflate(
                    R.layout.item, null);
            itemHolder = new ItemHolder();
            itemHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) convertView.getTag();
        }

      List<InspectItem> contents =   list.get(groupPosition).getItems();
       final InspectItem item = contents.get(childPosition);
        if(contents != null)  {
            itemHolder.name_tv.setText(item.getInspectItemName());
        }
        final ItemHolder finalItemHolder = itemHolder;

        if (checkMap.get(item.getInspectItemCode()) == null) {
            finalItemHolder.name_tv.setBackgroundColor(Color.TRANSPARENT);
            finalItemHolder.name_tv.setTextColor(Color.parseColor("#959595"));
        }else{


            finalItemHolder.name_tv.setBackgroundColor(Color.parseColor("#e8e8e8"));
            finalItemHolder.name_tv.setTextColor(Color.parseColor("#010101"));
        }

        itemHolder.name_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (checkMap.get(item.getInspectItemCode()) == null) {
                    //uncheck
                    finalItemHolder.name_tv.setBackgroundColor(Color.parseColor("#e8e8e8"));
                    finalItemHolder.name_tv.setTextColor(Color.parseColor("#010101"));
                    checkMap.put(item.getInspectItemCode(), true);
                    contentMap.put(item.getInspectItemCode(), item);
                } else {
                    Boolean b = checkMap.get(item.getInspectItemCode());
                    if (b) {
                        //checkede8e8e8
                        finalItemHolder.name_tv.setBackgroundColor(Color.TRANSPARENT);
                        finalItemHolder.name_tv.setTextColor(Color.parseColor("#959595"));
                        checkMap.put(item.getInspectItemCode(), false);
                        contentMap.remove(item.getInspectItemCode());

                    } else {
                        //uncheck
                        finalItemHolder.name_tv.setBackgroundColor(Color.parseColor("#e8e8e8"));
                        finalItemHolder.name_tv.setTextColor(Color.parseColor("#010101"));
                        checkMap.put(item.getInspectItemCode(), true);
                        contentMap.put(item.getInspectItemCode(), item);

                    }
                }
                if (callback != null) {
                    callback.call();
                }

            }
        });

        return convertView;
    }



    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class GroupHolder {
        public TextView name_tv;
    }

    class ItemHolder {
        public TextView name_tv;
    }

    public void setDataChanage(List<InspectItem> list){

        this.list = list;
        notifyDataSetChanged();

    }

    public Callback callback;
    public void setCallback(Callback call){
        this.callback = call;
    }

    public Map<String,InspectItem> getSelectMap(){

        return contentMap;
    }

    public void resetSelect(){
        contentMap.clear();
        checkMap.clear();
    }
}



